var app = getApp();
Page({
  data: {
    city: '北京市',
    hotelList: [],
    page:1,
    pagesize:10,
    hasMoreData:true
  },
  onLoad: function (options) {
  },
  onShow: function () {
    this.setData({
      page: 1,
      city: app.globalData.currentCity
    })
    this.loadData()
  },
  loadData() {
    let self = this
    app.request({
      url: '/home/hotel/index',
      method: 'POST',
      data: {
        city: self.data.city,
        page: self.data.page,
        pagesize: self.data.pagesize
      },
      success: (res) => {
        var listTemp = self.data.hotelList
        if (self.data.page == 1) {
          listTemp = []
          self.setData({
            dataList: []
          })
        }
        if (res.result != null && res.result.length > 0) {
          var list = res.result
          for(var i = 0;i<list.length;i++){
            if (list[i].hotel_name.length > 16){
              list[i].hotel_name = list[i].hotel_name.substr(0,16) + "..."
            }
          }
          if(list.length < self.data.pagesize){
            self.setData({
              hotelList: listTemp.concat(list),
              hasMoreData:false
            })
          } else {
            self.setData({
              hotelList: listTemp.concat(list),
              hasMoreData: true,
              page: self.data.page + 1
            })
          }
        }
      }
    })
  },
  //下拉
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.loadData()
    wx.stopPullDownRefresh();
  },
  //上拉
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.loadData()
    } else {
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
    }
  }
})
